import { Bus, DAY_STATE } from "./Bus";
import { IBroadcast } from "./IBroadcast";

/**
 * 顶部币种的大盘数据
 */
export class DayBroadcast extends IBroadcast {
    // channelParam: any;
    channelStr = "";
    channelParam = {
        event: "subscribe",
        symbol: "ALL",
        topic: "ticker"
    };

    changeParam(symbol: string) {
        this.channelParam = {
            event: "subscribe",
            symbol: "ALL",
            topic: "ticker"
        };
        this.subscribe();
        // {"event":"subscribe","symbol":"ETCUSDT","channel":"ticker","interval":"S1"}
    }

    subscribe(): void {
        console.log("初始化订阅======================#################");
        this.unSubscribe();
        if (!this.channelParam) {
            console.log("订阅参数未初始化");
            return;
        }
        this.channelStr = JSON.stringify(this.channelParam);
        // console.error("day === sub", this.channelStr)
        // this.socket.send(this.channelStr);

        this.send(this.channelStr);
    }

    unSubscribe(): void {
        if (!this.channelStr) return;
        const obj = JSON.parse(this.channelStr);
        obj.event = "unsubscribe";
        this.send(JSON.stringify(obj));
    }

    handle(res: any): void {
        // console.log("##jsz", res, "subscribe symbol ticker");
        // throw new Error("Method not implemented.");
        // console.log(res.data);
        if (res.data) {
            Bus.emit(DAY_STATE, res.data);
        }
    }
}
